<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">编辑图书</div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="书名" prop="name">
        <el-input v-model="form.name" placeholder="请输入书名"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="form.author" placeholder="请输入作者"></el-input>
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input v-model="form.publisher" placeholder="请输入出版社"></el-input>
      </el-form-item>
      <el-form-item label="出版日期" prop="publishDate">
        <el-date-picker
            v-model="form.publishDate"
            type="date"
            value-format="yyyy-MM-dd"
            style="width: 193px"
            placeholder="选择出版日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-cascader
            v-model="form.categories"
            :options="categories"
            :props="{value:'name',label:'name'}">
        </el-cascader>
      </el-form-item>
      <el-form-item label="标准码" prop="bookNo">
        <el-input v-model="form.bookNo" placeholder="请输入标准码"></el-input>
      </el-form-item>
      <el-form-item label="积分" prop="score">
        <el-input-number v-model="form.score" :min="10" :max="30" label="所需积分"></el-input-number>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input style="width: 300px;" type="textarea" v-model="form.description" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:9090/api/book/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


    </el-form>

    <div style="text-align: center;margin-top: 30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
    </div>

  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "EditBook",
  data(){
    return{
      imageUrl: '',
      form:{},
      categories:[],
      rules: {
        username: [{ required: true, message: '请输入图书名称', trigger: 'blur' }],
        score:[{required:true,message:"请输入借书积分",trigger:"blur"}],
        bookNo:[{required:true,message:"请输入标准码",trigger:"blur"}]
      }
    }
  },
  created() {
    request.get("/category/tree").then(res => {
          this.categories = res.data
        }
    )
    const id = this.$route.query.id
    request.get("/book/"+id).then(res =>{
      this.form = res.data
      if (this.form.category){
        this.form.categories = this.form.category.split(">")
      }
    })
  },
  methods:{
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
      console.log(file.type)
      console.log(isJPG)
      console.log(isPNG)
      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
    },
    handleAvatarSuccess(res,file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.cover = res.data
    },
    save(){
      request.put("/book/update",this.form).then(res => {
        if (res.code == '200'){
          this.$notify.success('操作成功！')
          this.$router.push("/bookList")
        }else {
          this.$notify.error(res.msg)
        }
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
